package com.xinya.wanandroidclient.ui.tologin

import android.os.Bundle
import androidx.databinding.ViewDataBinding
import androidx.fragment.app.Fragment
import androidx.viewpager2.adapter.FragmentStateAdapter
import com.google.android.material.tabs.TabLayout
import com.google.android.material.tabs.TabLayoutMediator
import com.xinya.wanandroidclient.R
import com.xinya.wanandroidclient.adapter.LoginBannerAdapter
import com.xinya.wanandroidclient.base.BaseFragment
import com.xinya.wanandroidclient.databinding.FragmentTabBinding
import com.xinya.wanandroidclient.databinding.FragmentToLoginBinding
import com.xinya.wanandroidclient.ui.tologin.login.LoginFragment
import com.xinya.wanandroidclient.ui.tologin.register.RegisterFragment

/**
 *
 * @ClassName:      LoginFragment$
 * @Description:    java类作用描述
 * @Author:         xinya
 * @CreateDate:     2021/2/24$ 16:56$
 * @UpdateRemark:   更新内容
 * @Version:        1.0
 */
class ToLoginFragment : BaseFragment() {
    override fun getLayoutId() = R.layout.fragment_to_login

    private lateinit var dataBinding: FragmentToLoginBinding
    override fun initDataBinding(binding: ViewDataBinding?) {
        dataBinding = binding as FragmentToLoginBinding
    }

    override fun init(savedInstanceState: Bundle?) {
        val toLoginBannerView = dataBinding.toLoginBanner
        val toLoginTabView = dataBinding.toLoginTab
        val toLoginViewpagerView = dataBinding.toLoginViewpager

        toLoginViewpagerView.apply {
            adapter = object : FragmentStateAdapter(this@ToLoginFragment) {
                override fun getItemCount(): Int {
                    return 2
                }

                override fun createFragment(position: Int): Fragment {
                    return when (position) {
                        0 -> {
                            LoginFragment()
                        }
                        else -> {
                            RegisterFragment()
                        }
                    }
                }
            }

            offscreenPageLimit = 1
        }

        TabLayoutMediator(toLoginTabView, toLoginViewpagerView) { tab: TabLayout.Tab, position: Int ->
            when (position) {
                0 -> {
                    tab.text = getString(R.string.login_text)
                }
                else -> {
                    tab.text = getString(R.string.register_text)
                }
            }
        }.attach()

        val loginBannerDataList = mutableListOf<Int>()
        loginBannerDataList.add(R.mipmap.login_1)
        loginBannerDataList.add(R.mipmap.login_2)

        toLoginBannerView.apply {
            adapter = LoginBannerAdapter(loginBannerDataList)
            addBannerLifecycleObserver(viewLifecycleOwner)
        }



    }
}